package controller;


import BBS.bean.PostInfo;
import service.PostService;
import service.impl.PostServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/post")
public class PostController extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String type = request.getParameter("type");
        if(type==null){
            response.sendRedirect("page.jsp");
        }
        if(type.equals("findPostByPageId")){
            findPostByPageId(request,response);
        }
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    private void findPostByPageId(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer pageId = Integer.parseInt(request.getParameter("pageId"));
        PostService ps = new PostServiceImpl(request.getServletContext());
        List<PostInfo> list = ps.findPostByPageId(pageId);
        if(list!=null){
            request.getSession().setAttribute("POSTS",list);
            response.sendRedirect("page_info.jsp?msg=ok");
        }else{
            response.sendRedirect("page.jsp");
        }
    }


}
